/* eslint-disable */
import request from '@/request'

/** 此处后端没有提供注释 POST /route/add */
export async function addRoute(body: API.addRouteParams, options?: { [key: string]: any }) {
  console.log('🚀 addRoute 被调用，参数:', body)

  // 构建FormData，因为包含文件上传
  const formData = new FormData()

  // 添加基本字段
  if (body.routeName) formData.append('routeName', body.routeName)
  if (body.routeInfo) formData.append('routeInfo', body.routeInfo)
  if (body.spendTime) formData.append('spendTime', body.spendTime)
  if (body.suitableTime) formData.append('suitableTime', body.suitableTime)

  // 添加沿途景点（数组）
  if (body.alongScenics && body.alongScenics.length > 0) {
    body.alongScenics.forEach((scenicId) => {
      formData.append('alongScenics', scenicId.toString())
    })
  }

  // 添加路线标签（数组）
  if (body.routeTags && body.routeTags.length > 0) {
    body.routeTags.forEach((tag) => {
      formData.append('routeTags', tag)
    })
  }

  // 添加封面图片
  if (body.routeCoverImage) {
    formData.append('routeCoverImage', body.routeCoverImage)
    console.log('📸 添加路线封面图片到FormData:', {
      name: body.routeCoverImage.name,
      size: body.routeCoverImage.size,
      type: body.routeCoverImage.type,
      isFile: body.routeCoverImage instanceof File,
    })
  } else {
    console.warn('⚠️ 路线封面图片为空!')
  }

  // 打印FormData内容用于调试
  console.log('📋 FormData 内容:')
  for (let [key, value] of formData.entries()) {
    if (value instanceof File) {
      console.log(`  ${key}:`, `文件(${value.name}, ${value.size}字节, ${value.type})`)
    } else {
      console.log(`  ${key}:`, value)
    }
  }

  console.log('📤 发送FormData到后端')

  return request<API.BaseResponseLong>('/route/add', {
    method: 'POST',
    data: formData,
    ...(options || {}),
  })
}

/** 此处后端没有提供注释 POST /route/delete */
export async function deleteRoute(body: API.DeleteRequest, options?: { [key: string]: any }) {
  return request<API.BaseResponseBoolean>('/route/delete', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  })
}

/** 获取路线分页列表 GET /route/page/list/vo */
export async function getListRouteVo(
  // 叠加生成的Param类型 (非body参数swagger默认没有生成对象)
  params: API.getListRouteVOParams,
  options?: { [key: string]: any },
) {
  console.log('🔍 调用路线分页查询接口，参数:', params)
  return request<API.BaseResponseRoutePageVO>('/route/page/list/vo', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  })
}

/** 此处后端没有提供注释 POST /route/update */
export async function updateRoute(body: API.updateRouteParams, options?: { [key: string]: any }) {
  console.log('🚀 updateRoute 被调用，参数:', body)

  // 构建FormData，因为可能包含文件上传
  const formData = new FormData()

  // 添加基本字段（必须字段都要添加，即使为空字符串）
  if (body.id !== undefined) formData.append('id', body.id.toString())
  if (body.routeName !== undefined) formData.append('routeName', body.routeName)
  if (body.routeInfo !== undefined) formData.append('routeInfo', body.routeInfo)
  if (body.spendTime !== undefined) formData.append('spendTime', body.spendTime)
  if (body.suitableTime !== undefined) formData.append('suitableTime', body.suitableTime)

  // 添加沿途景点（数组） - 确保清空原有数据并重新设置
  if (body.alongScenics !== undefined) {
    if (body.alongScenics.length > 0) {
      body.alongScenics.forEach((scenicId) => {
        formData.append('alongScenics', scenicId.toString())
      })
      console.log('📍 设置沿途景点:', body.alongScenics)
    } else {
      // 如果是空数组，也需要明确告知后端
      formData.append('alongScenics', '')
      console.log('📍 清空沿途景点')
    }
  }

  // 添加路线标签（数组） - 确保清空原有数据并重新设置
  if (body.routeTags !== undefined) {
    if (body.routeTags.length > 0) {
      body.routeTags.forEach((tag) => {
        formData.append('routeTags', tag)
      })
      console.log('🏷️ 设置路线标签:', body.routeTags)
    } else {
      // 如果是空数组，也需要明确告知后端
      formData.append('routeTags', '')
      console.log('🏷️ 清空路线标签')
    }
  }

  // 添加封面图片（如果有新图片）
  if (body.routeCoverImage) {
    formData.append('routeCoverImage', body.routeCoverImage)
    console.log('📸 添加新的路线封面图片到FormData:', {
      name: body.routeCoverImage.name,
      size: body.routeCoverImage.size,
      type: body.routeCoverImage.type,
      isFile: body.routeCoverImage instanceof File,
    })
  }

  // 打印FormData内容用于调试
  console.log('📋 更新FormData 内容:')
  for (let [key, value] of formData.entries()) {
    if (value instanceof File) {
      console.log(`  ${key}:`, `文件(${value.name}, ${value.size}字节, ${value.type})`)
    } else {
      console.log(`  ${key}:`, value)
    }
  }

  console.log('📤 发送更新FormData到后端')

  return request<API.BaseResponseBoolean>('/route/update', {
    method: 'POST',
    data: formData,
    ...(options || {}),
  })
}

/** 推荐路线列表 GET /route/recommend/list/vo */
export async function getRecommendRouteList(options?: { [key: string]: any }) {
  return request<API.BaseResponseListRouteVO>('/route/recommend/list/vo', {
    method: 'GET',
    ...(options || {}),
  })
}
